<template>
  <div class="app-container">
   
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="员工姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
        
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        

        <el-select
              v-model="queryParams.deptId"
              placeholder="请选择部门" @change="selectChange" style="width:100%"
            >
              <el-option v-for="item in deptlist" :key="item.deptId" :value="item.deptId" :label="item.deptName"/>
            </el-select>
     
      </el-form-item>



      <el-form-item label="月份" prop="month">
          <el-date-picker
          format="yyyy/MM"
          value-format='yyyyMM'
            type="month"
            placeholder='请选择月份'
            v-model="queryParams.month"
            @change="handleQuery"
          />
        </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!-- 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hrmanagement:salary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hrmanagement:salary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hrmanagement:salary:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hrmanagement:salary:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    <!-- </el-row> -->

    <el-table v-loading="loading" :data="salaryList" @selection-change="handleSelectionChange">
      <el-table-column prop="staffId" label="工号" min-width="125" align="center"/>
        <el-table-column prop="name" label="姓名" min-width="125" align="center"/>
        <el-table-column prop="deptName" label="部门" min-width="125" align="center"/>
        <el-table-column prop="phone" label="电话" min-width="125" align="center"/>
        <el-table-column label="扣款项目" align="center">
          <el-table-column prop="lateDeduct" label="迟到" min-width="125" align="center"/>
          <el-table-column prop="leaveEarlyDeduct" label="早退" min-width="125" align="center"/>
          <el-table-column prop="absenteeismDeduct" label="旷工" min-width="125" align="center"/>
          <el-table-column prop="leaveDeduct" label="请假" min-width="125" align="center"/>
        </el-table-column>
        <el-table-column label="缴纳项目" align="center">
          <el-table-column prop="housePay" label="公积金" min-width="125" align="center"/>
          <el-table-column prop="socialPay" label="社保" min-width="125" align="center"/>
        </el-table-column>
        <el-table-column label="实发工资" align="center">
          <el-table-column prop="baseSalary" label="基础工资" min-width="125" align="center"/>
          <el-table-column prop="subsidy" label="生活补贴" min-width="125" align="center"/>
          <el-table-column prop="bonus" label="奖金" min-width="125" align="center"/>
        </el-table-column>
        <el-table-column prop="totalSalary" label="最终工资" min-width="125" align="center"/>
        <el-table-column prop="remark" label="备注" min-width="200" align="center"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hrmanagement:salary:edit']"
          >详情设置</el-button>
         
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-drawer
  :title="title"
  :visible.sync="open"
  direction="rtl"
  size="45%"
  >
  <div style="padding-right: 30px;">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
        <el-form-item label="员工" prop="staffId" >
          <el-select v-model="form.staffId" disabled>
    <el-option
      v-for="item in alluserselect"
      :key="item.value"
      :label="item.label"
      :value="item.value"
      
      >
    </el-option>
  </el-select>
          </el-form-item>
          <el-form-item label="基本工资" prop="baseSalary">
          <el-input-number v-model="form.baseSalary" :min="0" style="width:48%" :precision="3"
          />
        </el-form-item>
        <el-form-item label="生活补贴" prop="subsidy">
          <el-input-number v-model="form.subsidy" :min="0" style="width:48%" :precision="3"
          />
        </el-form-item>
        <el-form-item label="奖金" prop="bonus">
          <el-input-number v-model="form.bonus" :min="0" style="width:48%" :precision="3"
          />
        </el-form-item>
        <el-form-item label="扣款" style="height:80px">
          <el-form-item style="height:30px">
            <el-form-item label="迟到" label-width="60px" style="display:inline-block">
              <el-input-number v-model="form.lateDeduct" :disabled="true" :controls="false"
                               :precision="3"/>
            </el-form-item>
            <el-form-item label="早退" label-width="80px" style="display:inline-block">
              <el-input-number v-model="form.leaveEarlyDeduct" :disabled="true" :controls="false"
                               :precision="3"/>
            </el-form-item>
          </el-form-item>
          <el-form-item style="height:30px">
            <el-form-item label="旷工" label-width="60px" style="display:inline-block">
              <el-input-number v-model="form.absenteeismDeduct" :disabled="true" :controls="false"
                               :precision="3"/>
            </el-form-item>
            <el-form-item label="请假" label-width="80px" style="display:inline-block">
              <el-input-number v-model="form.leaveDeduct" :disabled="true" :controls="false"
                               :precision="3"/>
            </el-form-item>
          </el-form-item>
        </el-form-item>
        <el-form-item label="缴费" style="height:30px">
          <el-form-item label="社保" label-width="60px" style="display:inline-block">
            <el-input-number
              v-model="insurance.perSocialPay"
              :disabled="true"
              :controls="false"
              :precision="3"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="公积金" label-width="80px" style="display:inline-block">
            <el-input-number
              v-model="insurance.perHousePay"
              :disabled="true"
              :controls="false"
              :precision="3"
            ></el-input-number>
          </el-form-item>
        </el-form-item>
        <el-form-item label="最终工资" prop="totalSalary">
          <el-input-number v-model="form.totalSalary" style="width:48%" :disabled="true"
                           :controls="false" :precision="3"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}"
                    v-model.trim="form.remark"
                    maxlength="100"
                    show-word-limit style="width:48%"/>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" style="text-align: right;margin-right: 20px;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
  </div>

</el-drawer>

  </div>
</template>

<script>
import { listSalary, getSalary, delSalary, addSalary, updateSalary } from "@/api/hrmanagement/salary";
import { getInsuranceByStaffId } from "@/api/hrmanagement/insurance";
import { AllDept } from "@/api/system/dept";
import { selectAllUser } from "@/api/system/user";
export default {
  name: "Salary",


  watch: {
    'form.baseSalary': function () {
      this.calculateSalary()
    },
    'form.subsidy': function () {
      this.calculateSalary()
    },
    'form.bonus': function () {
      this.calculateSalary()
    }
  },



  data() {
    return {insurance: {},
      deptlist:[],
    // 总工资
  alluser:[],
  alluserselect:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工工资表格数据
      salaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    AllDept().then(response => {
          if (response.code === 200) {
            this.deptlist= response.data
          } else {
            this.$message.error(response.message)
          }
        })
    selectAllUser().then(response => {
this.alluserselect = response.data
      console.log(this.alluser);
      });
    this.getList();
  },
  methods: {
    calculateSalary () {
      this.form.totalSalary = this.form.baseSalary +
        this.form.subsidy + this.form.bonus -
        this.insurance.perHousePay - this.insurance.perSocialPay - this.form.lateDeduct -
        this.form.leaveEarlyDeduct -
        this.form.leaveDeduct - this.form.absenteeismDeduct
    },
    /** 查询员工工资列表 */
    getList() {
      this.loading = true;
   
      listSalary(this.queryParams).then(response => {
        this.salaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staffId: null,
        baseSalary: 0,
        overtimeSalary: 0,
        subsidy: 0,
        bonus: 0,
        totalSalary: 0,
        lateDeduct: 0,
        leaveDeduct: 0,
        leaveEarlyDeduct: 0,
        absenteeismDeduct: 0,
        month: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    selectChange(){
     this.handleQuery()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工工资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      this.open = true;
              this.title = "修改员工工资";
     
      getInsuranceByStaffId(row.staffId).then(response => {
        if (response.code === 200) {
          this.insurance = response.data
          console.log(row);
          console.log("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
          console.log(response.data);
          
          this.form = {
            salaryId:row.salaryId,
            staffId: row.staffId,
            baseSalary: row.baseSalary,
            subsidy: row.subsidy,
            bonus: row.bonus,
            month: row.month,
            lateDeduct: row.lateDeduct,
            leaveDeduct: row.leaveDeduct,
            leaveEarlyDeduct: row.leaveEarlyDeduct,
            absenteeismDeduct: row.absenteeismDeduct,
            remark: row.remark
          }
        } else {
          this.$message.error('请先为此员工设置社保！')
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      console.log("sfdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
          console.log(this.form);
      this.form.totalSalary = this.totalSalary
      this.$refs["form"].validate(valid => {
 if (this.form.salaryId!==null) {
  updateSalary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
 }else{
  addSalary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
 }
            
   
           
      
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工工资编号为"' + ids + '"的数据项？').then(function() {
        return delSalary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hrmanagement/salary/export', {
        ...this.queryParams
      }, `salary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
